package com.J1ay.carsys.utils;
import com.J1ay.carsys.pojo.Bower;
import com.J1ay.carsys.pojo.Car;
import com.J1ay.carsys.pojo.Result;
import com.mysql.jdbc.StringUtils;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import ma.glasnost.orika.impl.util.StringUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @Author J1ay
 * @Date 2021/6/3 21:08
 * @Description
 */

public class Common {
    public static Result resultReturn(Object data) {
        return new Result(data, 200, "操作成功");
    }

    public static Result resultReturn(Object data, int status, String msg){
        return new Result(data,status,msg);
    }

    public static DefaultMapperFactory getMapperFactory() {
        return new DefaultMapperFactory.Builder().build();
    }


    //    身份证
    public static boolean isIDNumber(String IDNumber) {
        if (IDNumber == null || "".equals(IDNumber)) {
            return false;
        }
        // 定义判别用户身份证号的正则表达式（15位或者18位，最后一位可以为字母）
        String regularExpression = "(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|" +
                "(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)";
        boolean matches = IDNumber.matches(regularExpression);
        if (matches) {
            if (IDNumber.length() == 18) {
                try {
                    char[] charArray = IDNumber.toCharArray();
                    int[] idCardWi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
                    String[] idCardY = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
                    int sum = 0;
                    for (int i = 0; i < idCardWi.length; i++) {
                        int current = Integer.parseInt(String.valueOf(charArray[i]));
                        int count = current * idCardWi[i];
                        sum += count;
                    }
                    char idCardLast = charArray[17];
                    int idCardMod = sum % 11;
                    if (idCardY[idCardMod].toUpperCase().equals(String.valueOf(idCardLast).toUpperCase())) {
                        return true;
                    } else {
                        return false;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }
            }

        }
        return matches;
    }

    //    手机号校验
    public static boolean checkPhone(String in) {
        Pattern pattern = Pattern.compile("^[1]\\d{10}$");
        return pattern.matcher(in).matches();
    }

    public static String padString(String str, int leng) {
        String s = String.format("%1$22s",str);
//        if (leng-str.length() >=0){
//            for(int i=0;i<leng-str.length();i++){
//                s = s + " ";
//            }
//        }


        return s;
    }

    public static int getDayDiffer(Date startDate, Date endDate){
        int days = (int) ((endDate.getTime() - startDate.getTime()) / (1000*3600*24));
        return days;
    }
}
